@echo off
REM sign3.cmd by Xia Zuoling, 2009-9-11 20:19:08

SetLocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
REM ======================================================================

set PROGRAM=%~nx0
set signdir=D:\zerox\mobile\Nokia\sign3
set signsis_exe=%signdir%\signsis.exe
REM default target phone model: E51
set cert_file=%signdir%\E51_DOSPY_20090910001.cer
set key_file=%signdir%\E51_DOSPY_20090608.key

echo =================================================
echo * Nokia Symbian Installation File Signing Script
echo * NOTE: script was written for my Nokia E51/E66
echo *                                 by Xia Zuoling
echo *                                    2009-9-22
echo =================================================
if "%~1"=="" call :usage&&exit /b -1

set opt_info=0
set opt_unsign=0
set opt_pause=0
set opt_model=E51

:get_opts
echo %~1 | findstr /R "^-" >NUL
if errorlevel 1 goto main_sub
if "%~1"=="-i" set opt_info=1
if "%~1"=="-u" set opt_unsign=1
if "%~1"=="-p" set opt_pause=1
if "%~1"=="-51" set opt_model=0
if "%~1"=="-1" set opt_model=E51
if "%~1"=="-6" set opt_model=E66
if "%~1"=="-66" set opt_model=E66
shift
goto :get_opts

REM main routine
:main_sub

REM there must be an input file given
if "%~1"=="" call :usage&&exit /b -1
set opt_input=%~1
if not exist "!opt_input!" echo ERROR: input not exists: !opt_input!&&goto end

REM check which phone model is targeted
if "!opt_model!"=="E66" (
    set key_file=%signdir%\E66_DOSPY_20090928003.cer
    set key_file=%signdir%\E51_DOSPY_20090608.key
)

REM optional output file designation in command line
if "%~2"=="" (
    set opt_output=%~n1
    set opt_output=!opt_output:.unsign=.sign!
    set opt_output=!opt_output:-unsign=-sign!
    set opt_output=!opt_output:_unsign=_sign!
    set opt_output=!opt_output: unsign=_sign!
    echo !opt_output! | findstr /I "sign" >NUL
    if errorlevel 1 (
        set opt_output=!opt_output!_signed%~x1
    ) else (
        set opt_output=!opt_output!%~x1
    )
) else (
    set opt_output=%~2
)

REM -i switch
if !opt_info!==1 (
    echo.
    echo SIS file information...
    %signsis_exe% -o "!opt_input!"
    goto end
)
REM -u switch
if !opt_unsign!==1 (
    set opt_output=!opt_output:sign=unsign!
    echo.
    echo Unsigning file...
    echo  INPUT: !opt_input!
    echo OUTPUT: !opt_output!
    if exist !opt_output! echo ERROR: !opt_output!: file exists or is itself&&goto end
    %signsis_exe% -u "!opt_input!" "!opt_output!"
    goto end
)

REM just signing
echo Cer. file: %cert_file%
echo Key  file: %key_file%
echo.
echo Signing file...
echo  INPUT: !opt_input!
echo OUTPUT: !opt_output!
echo.

if exist "!opt_output!" echo ERROR: !opt_output!: output file already exists&&goto end
REM add model string to output file
if !opt_model!==E51 set opt_output=!opt_output:sign=E51sign!
if !opt_model!==E66 set opt_output=!opt_output:sign=E66sign!
%signsis_exe% "!opt_input!" "!opt_output!" "%cert_file%" %key_file%

REM ======================================================================
goto end

REM Subroutines
:usage
echo Usage:
echo %PROGRAM% [-i] [-u] FILE.sis [OUT.sis]
echo DESCRIPTION
echo   Default behavior: sign FILE.sis and output as OUT.sis.
echo   If OUT.sis were not provided, FILE_signed.sis will be output
echo OPTIONS
echo   -i    Output information on FILE.sis and exit
echo   -u    Remove last certificate installed on FILE.sis
goto :eof
REM ----------------------------------

goto end
REM ======================================================================
:end
if !opt_pause!==1 (pause) else (echo.)
EndLocal

